<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1-2 循环事件绑定：基于自定义属性</title>
</head>

<body>
  <button>第1个按钮</button>
  <button>第2个按钮</button>
  <button>第3个按钮</button>

  <script>
    let buttons = document.querySelectorAll("button");
    for (var i = 0; i < buttons.length; i++) {
      buttons[i].myIndex = i
      buttons[i].onclick = function () {
        console.log(this.myIndex)
      }
    }
  </script>
</body>

</html>